package com.jolink.omiis.wechat.message;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jolink.omiis.wechat.api.IWeChatBrokerMessage;
import com.jolink.omiis.wechat.api.impl.WeChatApiBaseService;
import com.jolink.omiis.wechat.inner.IOmiisMessager;
import com.jolink.omiis.wechat.message.bean.request.MessageRequestBase;
import com.jolink.omiis.wechat.message.bean.request.TextMessageRequest;
import com.jolink.omiis.wechat.message.type.MessageType;
import com.jolink.omiis.wechat.outer.WeChatResponse;
import com.jolink.omiis.wechat.util.XmlUtil;

@Service
public abstract class AbstractMessageHandler extends WeChatApiBaseService implements IMessageHandler {

	private String reqMessage = null;

	/**
	 * 中继消息发送接口
	 */
	private IWeChatBrokerMessage broker = null;
	private String respMessage = null;

	private Map<String, String[]> reqParams;

	private MessageRequestBase messageRequestBase;

	private MessageType messageType;

	@Autowired
	private IOmiisMessager messager;

	public AbstractMessageHandler() {

	}

	protected MessageRequestBase getMessageRequestBase() {
		return messageRequestBase;
	}

	/**
	 * @return the reqMessage
	 */
	public String getReqMessage() {
		return reqMessage;
	}

	/**
	 * @param reqMessage
	 *            the reqMessage to set
	 */
	public void setReqMessage(String reqMessage) {
		this.reqMessage = reqMessage;
	}

	/**
	 * @return the respMessage
	 */
	public String getRespMessage() {
		return respMessage;
	}

	/**
	 * @param respMessage
	 *            the respMessage to set
	 */
	public void setRespMessage(String respMessage) {
		this.respMessage = respMessage;
	}

	protected WeChatResponse createDefaultWeChatResponse(ResponseCode code) {
		WeChatResponse wr = new WeChatResponse();
		wr.setCode(code);
		return wr;
	}

	protected WeChatResponse createDefaultWeChatResponse() {
		return createDefaultWeChatResponse(ResponseCode.OK);
	}

	/**
	 * @return the reqParams
	 */
	public Map<String, String[]> getReqParams() {
		return reqParams;
	}

	/**
	 * @param reqParams
	 *            the reqParams to set
	 */
	public void setReqParams(Map<String, String[]> reqParams) {
		this.reqParams = reqParams;
	}

	/**
	 * @return the messager
	 */
	public IOmiisMessager getMessager() {
		return messager;
	}

	/**
	 * @param messager
	 *            the messager to set
	 */
	public void setMessager(IOmiisMessager messager) {
		this.messager = messager;
	}

	public IWeChatBrokerMessage getBroker() {
		return broker;
	}

	public void setBroker(IWeChatBrokerMessage broker) {
		this.broker = broker;
	}

	/**
	 * 将腾讯发送的xml格式消息转换成对应的消息实体
	 * 
	 * @see {@link MessageRequestBase}} {@link TextMessageRequest
	 *      ImageMessageRequest VideoMessageRequest VoiceMessageRequest}
	 * 
	 * @param xmlString
	 * @param clazz
	 * @return
	 */
	protected MessageRequestBase transfer2Obj(String xmlString, Class<?> clazz) {
		XmlUtil<MessageRequestBase> xml = new XmlUtil<>();
		return xml.xml2Obj(xmlString, clazz);
	}

	protected MessageRequestBase transfer2Obj(String xmlString) {
		XmlUtil<MessageRequestBase> xml = new XmlUtil<>();
		return xml.xml2Obj(xmlString, messageType.getBaseMessageRequest());
	}

	public MessageType getMessageType() {
		return messageType;
	}

	public void setMessageType(MessageType messageType) {
		this.messageType = messageType;
		messageRequestBase = transfer2Obj(reqMessage);
	}
}
